using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityEngine.Events;

public class BtnItem : MonoBehaviour, IPointerClickHandler
{
    public Button m_BtnItem;
    public Text m_MineCount;
    public Image m_MineImg;
    public Image m_Cover;
    public Image m_Flag;
    public Image m_Tip;
    public Action<int> m_ClickCallback;

    public void Reset()
    {
        m_MineCount.gameObject.SetActive(false);
        m_MineImg.gameObject.SetActive(false);
        m_Flag.gameObject.SetActive(false);
        m_Tip.gameObject.SetActive(false);
        m_Cover.gameObject.SetActive(true);
    }

    public void ShowMineCount(int count)
    {
        m_MineCount.text = count.ToString();
        m_MineCount.gameObject.SetActive(true);
        m_MineImg.gameObject.SetActive(false);
        m_Flag.gameObject.SetActive(false);
        m_Tip.gameObject.SetActive(false);
        m_Cover.gameObject.SetActive(false);
    }

    public void ShowMineCount(string content)
    {
        m_MineCount.text = content.ToString();
        m_MineCount.gameObject.SetActive(true);
        m_MineImg.gameObject.SetActive(false);
        m_Flag.gameObject.SetActive(false);
        m_Tip.gameObject.SetActive(false);
        m_Cover.gameObject.SetActive(false);
    }

    public void ShowMine()
    {
        m_MineCount.gameObject.SetActive(false);
        m_MineImg.gameObject.SetActive(true);
        m_Cover.gameObject.SetActive(false);
        m_Flag.gameObject.SetActive(false);
        m_Tip.gameObject.SetActive(false);
    }

    public void Open()
    {
        m_MineCount.gameObject.SetActive(false);
        m_MineImg.gameObject.SetActive(false);
        m_Flag.gameObject.SetActive(false);
        m_Tip.gameObject.SetActive(false);
        m_Cover.gameObject.SetActive(false);
    }

    public void Close()
    {
        m_MineCount.gameObject.SetActive(false);
        m_MineImg.gameObject.SetActive(false);
        m_Flag.gameObject.SetActive(false);
        m_Tip.gameObject.SetActive(false);
        m_Cover.gameObject.SetActive(true);
    }

    public bool IsOpen()
    {
        return m_Cover.gameObject.activeSelf == false;
    }

    public bool IsMark()
    {
        return m_Flag.gameObject.activeSelf == true;
    }

    public bool IsTip()
    {
        return m_Tip.gameObject.activeSelf == true;
    }

    public bool IsMineCount()
    {
        return m_MineCount.gameObject.activeSelf == true;
    }

    public bool IsEmpty()
    {
        return IsOpen() == true && IsMineCount() == false;
    }

    public bool MarkFlag()
    {
        m_MineCount.gameObject.SetActive(false);
        m_MineImg.gameObject.SetActive(false);
        m_Flag.gameObject.SetActive(m_Flag.gameObject.activeSelf == false);
        m_Tip.gameObject.SetActive(false);
        m_Cover.gameObject.SetActive(true);
        return m_Flag.gameObject.activeSelf == true;
    }

    public void MarkFlag(bool mark)
    {
        m_MineCount.gameObject.SetActive(false);
        m_MineImg.gameObject.SetActive(false);
        m_Flag.gameObject.SetActive(mark);
        m_Tip.gameObject.SetActive(false);
        m_Cover.gameObject.SetActive(true);
    }

    public bool MarkTip()
    {
        m_MineCount.gameObject.SetActive(false);
        m_MineImg.gameObject.SetActive(false);
        m_Flag.gameObject.SetActive(false);
        m_Tip.gameObject.SetActive(m_Tip.gameObject.activeSelf == false);
        m_Cover.gameObject.SetActive(true);
        return m_Tip.gameObject.activeSelf == true;
    }

    public void MarkTip(bool mark)
    {
        m_MineCount.gameObject.SetActive(false);
        m_MineImg.gameObject.SetActive(false);
        m_Flag.gameObject.SetActive(false);
        m_Tip.gameObject.SetActive(mark);
        m_Cover.gameObject.SetActive(true);
    }

    public void OnPointerClick(PointerEventData eventData)
    {
        if (eventData.button == PointerEventData.InputButton.Left)
        {
            OnBtnItemClick((int)PointerEventData.InputButton.Left);
        }
        else if (eventData.button == PointerEventData.InputButton.Right)
        {
            OnBtnItemClick((int)PointerEventData.InputButton.Right);
        }
        else
        {
            OnBtnItemClick((int)PointerEventData.InputButton.Middle);
        }
    }

    public EItemStatus GetItemStatus()
    {
        if (IsOpen()) return EItemStatus.eOpen;
        if (IsMark()) return EItemStatus.eMark;
        if (IsTip()) return EItemStatus.eTip;
        return EItemStatus.eNormal;
    }

    public void UpdateStatus(EItemStatus itemStatus, int value)
    {
        if (itemStatus == EItemStatus.eOpen)
        {
            if (value == 0)
                Open();
            else
                ShowMineCount(value);
        }
        else if (itemStatus == EItemStatus.eMark) MarkFlag();
        else if (itemStatus == EItemStatus.eTip) MarkTip();
        else
            Close();
    }

    //=========================================公私分界线==================================================
    void Awake()
    {
        m_MineCount.gameObject.SetActive(false);
        m_MineImg.gameObject.SetActive(false);
        m_Flag.gameObject.SetActive(false);
        m_Tip.gameObject.SetActive(false);
    }

    void OnBtnItemClick(int code)
    {
        if (m_ClickCallback != null) m_ClickCallback(code);
    }
}
